package com.kuhyygy.activity;

import com.kuhyygy.R;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.Toast;

public class BroadCastActivity extends Activity {
	
	private IntentFilter intentFilter;
	private SimpleBroadcast simpleReceiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_broad_cast);
		intentFilter = new IntentFilter();
		intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
		simpleReceiver = new SimpleBroadcast();
		registerReceiver(simpleReceiver, intentFilter);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(simpleReceiver);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.broad_cast, menu);
		return true;
	}
	
	class SimpleBroadcast extends BroadcastReceiver {
		
		@Override
		public void onReceive(Context context, Intent i) {
			ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo ni = connManager.getActiveNetworkInfo();
			if (null != ni && ni.isAvailable())
				Toast.makeText(context, "网络可用", Toast.LENGTH_LONG).show();
			else 
				Toast.makeText(context, "亲，网络不可用哟", Toast.LENGTH_LONG).show();
		}

	}

}
